/**
     *查询dtu列表与搜索框的模糊查询，有模糊查询，传dtu名称字段
     */
    var Grid = BUI.Grid,
        Store = BUI.Data.Store,
        columns = [
            {
                title: '序号', dataIndex: 'deviceId',width:'4%', renderer: function (value,obj,index) {
                    return index+1;
                }
            },
            {title: '设备名称', dataIndex: 'name',width:'10%'},
            {title: 'DTU名称', dataIndex: 'dtuCode',width:'9%'},
            {title: '地址码', dataIndex: 'addrCode',width:'9%'},
            {title: '站点性质', dataIndex: 'protocolType', width:'9%',renderer: function (value) {
                    switch (value){
                        case 1:
                            return '<p>流量计MOD51</p>';
                        case 2:
                            return '<p>开封流量计4.2</p>';
                        case 3 :
                            return '<p>宏沐压力表</p>';
                        case 4 :
                            return '<p>ct v1.1</p>';
                        case 5:
                            return '<p>ct v1.2</p>';
                    }
                }
            },
            {title: '设备状态', dataIndex: 'sts',width:'7%', renderer: function (value) {
                    return value==0?"<span style='color:#d9534f'>异常</span>":"<span>正常</span>";
                }
            },
            {title: '备注', dataIndex: 'remark',width:'9%', renderer: function (value) {
                    return value==""||value==null?"____":value;
                }
            },
            {title: '时间', dataIndex: 'createTime',width:'13%', renderer: function (value) {
                    return new Date(value).Format("yyyy-MM-dd hh:mm:ss");
                }
            },
            {
                title: '操作', dataIndex: 'deviceId',width:'18%', renderer: function (value) {
                    var option1 = "<a href='javascript:addCollectionDtu(" + value + ")'>采集设置</a>";
                    var option2= "<a href='javascript:addAlarmDtu(" + value + ")'>报警设置</a>";
                    var option3 ='<a href="/rest/web/device/toDeviceEdit?deviceId=' + value + '">编辑</a>';
                    var option4= '<a href="javascript: delOneDev(' + value + ');">删除</a>';
                    return option1 + "&nbsp;&nbsp;&nbsp;&nbsp;" + option2+ "&nbsp;&nbsp;&nbsp;&nbsp;" + option3+ "&nbsp;&nbsp;&nbsp;&nbsp;" + option4;
                }
            }
        ];

    //分页查询
    var store = new Store({
            url: ' /rest/web/device/selectDevicePage',
            autoLoad: true, //自动加载数据
            params: { //配置初始请求的参数
                length: '15',
                start: '0'
            },
            pageSize: 15,	// 配置分页数目
            root: 'data.list',
            totalProperty: 'data.totalCount'
        }),
        grid = new Grid.Grid({
            render: '#grid',
            columns: columns,
            loadMask: true, //加载数据时显示屏蔽层
            store: store,
            plugins: [Grid.Plugins.CheckSelection], // 插件形式引入多选表格
            // 底部工具栏
            bbar: {
                pagingBar: true
            }
        });

    grid.render();
    /**模糊搜索**/
    var form = new BUI.Form.HForm({
        srcNode: '#searchForm'
    }).render();

    form.on('beforesubmit', function (ev) {
        //序列化成对象
        var obj = form.serializeToObject();
        obj.start = 0; //返回第一页
        store.load(obj);
        return false;
    });
/**删除单个Dtu**/
function delOneDev(value) {
    BUI.Message.Confirm('<h4 class="title-p">提示信息</h4><p>您确认要删除吗？</p>', function () {
        $.ajax({
            type: "POST",
            url: "/rest/web/device/deleteDevice",
            dataType: "json",
            data: {
                deviceId: value
            },
            success: function (data) {
                window.location.href = "/rest/web/device/toDeviceList";
            }
        });
    }, 'question');
}

/**删除多个传感设备**/
function delBtn() {
    var selections = grid.getSelection();
    var ids = new Array();
    for (var i = 0; i < selections.length; i++) {
        ids[i] = selections[i].deviceId.toString()
    }
    BUI.Message.Confirm('<h4 class="title-p">提示信息</h4><p>您确认要删除吗？</p>', function () {
        $.ajax({
            type: "POST",
            url: " /rest/web/device/deleteDevices",
            dataType: "json",
            data: {
                ids: ids
            },
            success: function (data) {
                var obj = new Object();
                obj.start = 0; //返回第一页
                store.load(obj);
            }
        });
    }, 'question');
}
/**
 *添加采集设置
 */
function addCollectionDtu(id){
    $.ajax({
        type: "GET",
        dataType: "json",
        url: "/rest/web/device/selectDeviceDetail",
        data: {deviceId: id},
        success: function (data) {
            var form = $("#editCollectionForm")[0];
            BUI.FormHelper.setFields(form, data.data);
            editCollectionDialog.show();
        }
    });
}
var editCollectionForm = new BUI.Form.Form({
    srcNode: '#editCollectionForm',
    submitType: 'ajax',
    callback: function (data) {
        var obj = new Object();
        obj.start = 0; //返回第一页
        store.load(obj);
        editCollectionDialog.close();
    }
});
editCollectionForm.render();
var editCollectionDialog = new BUI.Overlay.Dialog({
    title: '采集设备',
    width: 500,
    height: 375,
    contentId: 'editCollectionContent',
    success: function () {
        if (editCollectionForm.isValid()) {
            editCollectionForm.submit();
        } else {
            editCollectionForm.valid();
        }
    }
});
/**
 *添加addAlarmDtu报警设置
 */
function addAlarmDtu(id){
    $.ajax({
        type: "GET",
        dataType: "json",
        url: "/rest/web/device/selectDeviceDetail",
        data: {deviceId: id},
        success: function (data) {
            var form = $("#editAlarmForm")[0];
            BUI.FormHelper.setFields(form, data.data);
            editAlarmDialog.show();
        }
    });
}
var editAlarmForm = new BUI.Form.Form({
    srcNode: '#editAlarmForm',
    submitType: 'ajax',
    callback: function (data) {
        var obj = new Object();
        obj.start = 0; //返回第一页
        store.load(obj);
        editAlarmDialog.close();
    }
});
editAlarmForm.render();
var editAlarmDialog = new BUI.Overlay.Dialog({
    title: '报警设备',
    width: 500,
    height: 500,
    contentId: 'editAlarmContent',
    success: function () {
        if (editAlarmForm.isValid()) {
            editAlarmForm.submit();
        } else {
            editAlarmForm.valid();
        }
    }
});
